package remove_nth_node

import (
	. "gocode/algorithm/utils"
)

func removeNthFromEnd(head *ListNode, n int) *ListNode {
	dummy := &ListNode{Next: head}
	pre, left, right := dummy, head, head
	for n > 1 {
		right = right.Next
		n--
	}
	for right.Next != nil {
		pre = left
		left = left.Next
		right = right.Next
	}
	pre.Next = left.Next
	return dummy.Next
}
